<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/5/4
 * Time: 20:28
 */

namespace app\shopping\model;

use app\common\model\Base;

class Commalltool extends Base{
    /**
     *  得到某个商品基本信息
     * @param str $comm_sort 查询的商品类别（这里会判断是不是在搜索商品）
     *
     */
    function getItemComm($comm_sort, $comm_id) {
        if ($comm_sort == 'home' || $comm_sort == 'consumption' || $comm_sort == 'integral' || $comm_sort == 'activity' || $comm_sort == 'merchant') {
            $sql = $this->getBasicsSQL($comm_sort) . " WHERE A.comm_id='$comm_id'";

            return $this->selectItemComm($sql);
        } else {
            $sql = $this->getBasicsSQL($comm_sort) . " WHERE comm_id='$comm_id'";
            return $this->selectItemComm($sql);
        }
    }

    /**
     *  得到所有商品基本信息
     * @param str $comm_sort 查询的商品类别（这里会判断是不是在搜索商品）
     *
     */
    function getAllComm($comm_sort) {
        if ($comm_sort == "home") {
            $sql = $this->getBasicsSQL($comm_sort);
            $this->selectCommHome($sql);
        } else if ($comm_sort == 'consumption' || $comm_sort == 'integral' || $comm_sort == 'merchant') {
            $sql = $this->getBasicsSQL($comm_sort) . " WHERE A.comm_visibility=0";
            $this->selectCommAll($sql);
        } else if ($comm_sort == 'activity') {
            $sql = $this->getBasicsSQL($comm_sort) . " WHERE A.comm_visibility=0 AND (((A.activity_type=1 OR A.activity_type=3) and A.start_time<=now() AND A.end_time>=now()) OR (A.activity_type<>1 AND A.activity_type<>3))";
            $this->selectCommAll($sql);
        } else {
            $sql = $this->getBasicsSQL($comm_sort) . " WHERE comm_visibility=0";
            $this->selectCommAll($sql);
        }
    }

    /**
     *  得到搜索商品基本信息
     * @param str $comm_sort 查询的商品类别（这里会判断是不是在搜索商品）
     *
     */
    function getSearchComm($comm_sort, $search) {
        if ($comm_sort == 'home' || $comm_sort == 'consumption' || $comm_sort == 'integral' || $comm_sort == 'merchant') {
            $sql = $this->getBasicsSQL($comm_sort) . " WHERE A.comm_visibility=0 AND comm_name LIKE '%" . $search . "%'";
            $this->selectCommAll($sql);
        } else if ($comm_sort == 'activity') {
            $sql = $this->getBasicsSQL($comm_sort) . " WHERE A.comm_visibility=0 AND (((A.activity_type=1 OR A.activity_type=3) and A.start_time<=now() AND A.end_time>=now()) OR (A.activity_type<>1 AND A.activity_type<>3)) AND comm_name LIKE '%" . $search . "%'";
            $this->selectCommAll($sql);
        } else {
            $sql = $this->getBasicsSQL($comm_sort) . " WHERE comm_visibility=0 AND comm_name LIKE '%" . $search . "%'";
            $this->selectCommAll($sql);
        }
    }

    function getBasicsSQL($comm_sort) {
        switch ($comm_sort) {
            case 'home'://查询首页商品
                return "SELECT A.category,A.comm_id,A.comm_img,B.comm_name,B.comm_title,B.comm_price,B.comm_integral,B.comm_discount_price FROM comm_home AS A LEFT JOIN comm AS B on A.comm_id = B.comm_id";
            case 'consumption'://查询消费专区商品
                return "SELECT A.comm_id,A.comm_price,A.comm_integral,A.comm_discount_price,B.comm_img,B.comm_name,B.comm_title FROM comm_consumption AS A LEFT JOIN comm AS B ON A.comm_id = B.comm_id";
            case 'integral'://查询基分专区商品
                return "SELECT A.comm_id,A.comm_price,A.comm_integral,A.comm_discount_price,B.comm_img,B.comm_name,B.comm_title FROM comm_integral AS A LEFT JOIN comm AS B ON A.comm_id = B.comm_id";
            case 'activity'://查询活动专区商品
                return "SELECT A.comm_id,A.comm_price,A.comm_integral,A.comm_discount_price,A.start_time,A.end_time,B.comm_img,B.comm_name,B.comm_title FROM comm_activity AS A LEFT JOIN comm AS B ON A.comm_id = B.comm_id";
            case 'merchant'://查询商户专区商品
                /*$uid = $_GET["uid"];
                if (empty($uid)) {
                    die(echoJson(0, "参数错误-23!"));
                }*/
                return "SELECT A.comm_id,A.comm_price,A.comm_integral,convert(A.comm_discount_price*(IFNULL((SELECT B.grade_discounts FROM `user` AS A LEFT JOIN agency_shop_grade AS B ON A.agency_grade_id=B.grade_id WHERE A.uid='$uid'),0)),decimal(10,2)) AS comm_discount_price,B.comm_img,B.comm_name,B.comm_title FROM comm_merchant AS A LEFT JOIN comm AS B ON A.comm_id = B.comm_id";
            case 'all'://查询商户专区商品
                return "SELECT comm_id,comm_price,comm_integral,comm_discount_price,comm_img,comm_name,comm_title FROM comm";
            default://查询所有商品
                return "SELECT comm_id,comm_price,comm_integral,comm_discount_price,comm_img,comm_name,comm_title FROM comm";
        }
    }

    /**
     *  查询首页商品
     */
    function selectCommHome($sql) {
        $result = $this->mysql->selectSQLAll($sql);
        if (is_array($result)) {
            $jsonArray = array();
            $categoryArr = array();
            $typeAll = array_unique(array_column($result, 'category')); //得到所有的类别
            foreach ($typeAll as $type) {
                $array = array();
                foreach ($result as $row) {
                    isset($row["category"]) ? $category = $row["category"] : $category = '';
                    if ($category == $type) {
                        $array[] = $this->getCommLtemArr($row);
                    }
                }
                if ($type == 'home' || $type == 'swiper') {
                    $jsonArray[$type] = $array;
                } else {
                    $categoryArr[] = array('name' => $type, 'value' => $array);
                }
            }
            if (is_array($categoryArr) && count($categoryArr) > 0) {
                $jsonArray['category'] = $categoryArr;
                echo echoJson(1, $jsonArray);
            } else {
                echo echoJson(-1, "暂无商品,-1");
            }
        } else {
            echo echoJson(-1, "暂无商品,-2");
        }
    }

    /**
     *   查询所有商品
     */
    function selectCommAll($sql) {
        $result = $this->mysql->selectSQLALL($sql);
        $jsonArray = array();
        if (is_array($result)) {
            foreach ($result as $row) {
                $jsonArray[] = $this->getCommLtemArr($row);
            }
            if (is_array($jsonArray) && count($jsonArray) > 0) {
                echo echoJson(1, $jsonArray);
            } else {
                echo echoJson(-1, "商品空空如也，-1!");
            }
        } else {
            echo echoJson(-1, "商品空空如也，-2!");
        }
    }

    /**
     *   查询所有商品
     */
    function selectItemComm($sql) {
        $row = $this->mysql->selectSQLRow($sql);
        if ($row) {
            return $this->getCommLtemArr($row);
        } else {
            return false;
        }
    }

    /**
     *
     * 得到商品基本信息(返回单独的某一个商品的信息)
     * $row 数据库的某一条记录
     *
     */
    function getCommLtemArr($row) {
        $comm_price = !empty($row['comm_price']) ? $row['comm_price'] : 0;
        $comm_discount_price = !empty($row['comm_discount_price']) ? $row['comm_discount_price'] : $comm_price;
        $comm_integral = !empty($row['comm_integral']) ? (int) $row['comm_integral'] : 0;
        return array("comm_id" => $row['comm_id'], "comm_name" => $row['comm_name'], "comm_img" => $row['comm_img'],
            "comm_title" => $row['comm_title'], "comm_price" => $comm_price, "comm_integral" => $comm_integral, "comm_discount_price" => $comm_discount_price);
    }

}